/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.terminalrodoviario.servlets;

import br.terminalrodoviario.Company;
import br.terminalrodoviario.Solicitation;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author TIAGO
 */
public class SolicitationCompanyServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String action = request.getParameter("acao");
            String id = request.getParameter("id");
            if (request.getParameter("delete.x") != null || request.getParameter("delete.y") != null) {
                delete(request, response);
            } else if ((request.getParameter("edit.x") != null || request.getParameter("edit.y") != null) && id != null) {
                response.sendRedirect("Fields/FSolicitation.jsp?id=" + id);
            } else if (action.equalsIgnoreCase("gravar") && id.equals("null")) {
                insert(request, response);
            } else if (action.equalsIgnoreCase("gravar") && !id.equals("null")) {
                update(request, response);
            }
        } catch (Exception e) {
            out.print(e.getMessage());
            e.printStackTrace(out);
        } finally {
            out.close();
        }
    }

    private void insert(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Solicitation n = fillObject(request, response);
        n.setStatus(0);
        n.insert();
        try {
            response.sendRedirect("Manager/MSolicitation.jsp");
        } catch (IOException ex) {
        }
    }

    private Solicitation fillObject(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Solicitation n = new Solicitation();
        Company c = new Company();
        c.setId(Integer.parseInt(request.getParameter("id_company")));
        n.setCompany(c);
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        df.setLenient(false);
        Date dt = null;
        try {
            dt = df.parse(request.getParameter("date_solicitation"));
        } catch (ParseException ex) {
            Logger.getLogger(SolicitationCompanyServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        n.setDateSolicitation(dt);
        n.setDescription(request.getParameter("txtDescription"));
        return n;
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) {
        String[] ids = request.getParameterValues("id");
        if (ids != null){
              for(int k = 0; k< ids.length; k++){
                Solicitation c = new Solicitation();
                    c.setId(Integer.parseInt(ids[k]));
                    c.delete();
              }
        }

        try{
            response.sendRedirect("Manager/MSolicitation.jsp");
        }catch(IOException ex){

        }
    }

    private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Solicitation n = fillObject(request, response);
        String id = request.getParameter("id");
        n.setId(Integer.parseInt(id));
        n.update();
        try {
            response.sendRedirect("Manager/MSolicitation.jsp");
        } catch (IOException ex) {
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
